package com.example.teamtribe.main.club.join;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import butterknife.BindView;

public class ApplyActivity extends BaseActivity implements ApplyContract.View, TextWatcher {

    @BindView(R.id.et_name) EditText etName;
    @BindView(R.id.et_no) EditText etNo;
    @BindView(R.id.et_academy) EditText etAcademy;
    @BindView(R.id.et_major) EditText etMajor;
    @BindView(R.id.et_phone) EditText etPhone;
    @BindView(R.id.et_qq) EditText etQQ;
    @BindView(R.id.bt_ok) Button btOk;
    ApplyPresenter presenter;
    private int teamId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        ((TextView) findViewById(R.id.tv_title)).setText("加入社团");
        User user = Utility.getUser();
        teamId = getIntent().getIntExtra("teamId", 0);
        if (teamId == 0) {
            finish();
        }
        etName.setText(user.getName());
        etNo.setText(user.getNo());
        etAcademy.setText(user.getAcademy());
        etMajor.setText(user.getMajor());
        etPhone.addTextChangedListener(this);
        etQQ.addTextChangedListener(this);
        presenter = new ApplyPresenter(this);
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.send(teamId, etPhone.getText().toString().trim(), etQQ.getText().toString().trim());
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        boolean flag1 = etPhone.getText().length() == 11;
        boolean flag2 = etQQ.getText().length() > 0;
        if (flag1 && flag2) {
            btOk.setBackground(getDrawable(R.drawable.button_stroke));
            btOk.setTextColor(getResources().getColor(R.color.colorAccent));
            btOk.setEnabled(true);
        } else {
            btOk.setEnabled(false);
            btOk.setTextColor(getResources().getColor(R.color.unable));
            btOk.setBackground(getDrawable(R.drawable.button_stroke_unable));
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void next() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("已发送申请");
            }
        });
        finish();
    }
}
